var wc = require("../components/winLocation.js");
import {
    initView,
    showMerQrCode,
    showLoadingDialog,
    hideLoadingDialog
} from '../views/merchantHomeView.js';
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var numberComponent = require("../components/number.js");

var token, loginName, openid, loginType, corpAbbr;
var errorObject;
$(document).on("pageinit", "#merchantHome_page", controllerInit);

function controllerInit() {
    console.log("dfd");
    wc.checkBrowser();
    initValue();
    initView();
    showLoadingDialog();
    getMer();
    merchantQuery();
    bindQrclick();
    bindSpendBtn();
}

// 通过营业执照号查询商户信息
function getMer() {
    var gmParameters = {
        "token": token,
        "bizLicNo": loginName,
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType
    };

    $.post(MerInfoServlet_URL, gmParameters, gmBack);
}

// 商户查询回调函数
function gmBack(gmData) {
    console.dir(gmData);
    if (gmData.lfq_merinfo_response) {
        var response = gmData.lfq_merinfo_response;
        var merinfo = response.mer_extra_info;
        var carid = response.cardId;
        carid = carid.replace("s", "");
        sessionStorage.setItem("acc_id", carid); // 取现 参数acc_id
        AccBalance();

    }
}

function AccBalance() {
    var abParameters = {
        "token": token,
        "accId": sessionStorage.getItem("acc_id"),
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType
    };

    $.post(AccBalanceServlet_URL, abParameters, abBack);
}

// 商户查询回调函数
function abBack(abData) {
    console.dir(abData);
    if (abData.ppcs_mulaccbalqry_response) {
        var response = abData.ppcs_mulaccbalqry_response;
        var balance = numberComponent.hold2bit(response.acc_balance / 100);
        $("#balance").text(balance);
        hideLoadingDialog();
        sessionStorage.setItem("balance", balance); // 取现账户余额      
        sessionStorage.setItem("valid_balance", response.valid_balance); // 取现 参数valid_balance
    }
}


// 余额 当日交易次数 当日交易金额 待结算金额 查询
function merchantQuery() {
    var mqParameters = {
        "token": token,
        "bizLicNo": loginName,
        "loginName": loginName,
        "openid": openid,
        "loginType": loginType
    };

    $.post(BalanceServlet_URL, mqParameters, mqBack);
}

// 商户查询回调函数
function mqBack(mqData) {
    if (mqData.login_response) {
        haslogin(mqData);
        return;
    }
    console.dir(mqData);
    if (mqData.mer_acct_response) {
        var theDate = changeDateTime(String(mqData.mer_acct_response.res_timestamp), "date");
        var response = mqData.mer_acct_response;
        //        $("#balance").text(response.balance);
        $("#djs_money").text(numberComponent.hold2bit(response.unsettled));
        $("#jy_no").text(response.trans_count);
        $("#jy_money").text(numberComponent.hold2bit(response.trans_amt));
        $("#banck-card").text(response.bank_id);
        $("#billText span:first-child").text(response.corp_abbr);
        corpAbbr = response.corp_abbr;
        $(".nowDate").text(theDate);
        hideLoadingDialog();
        if (response.balanceTime && response.balanceTime != "") {
            var formatbTime = changeDateTime(String(response.balanceTime), "date");
            $("#balance-time").text(formatbTime);
        }

        //        sessionStorage.setItem("balance", response.balance); // 取现账户余额
        //        sessionStorage.setItem("acc_id", response.acc_id.replace("s", "")); // 取现 参数acc_id
        sessionStorage.setItem("bank_id", response.bank_id); // 取现银行卡号
        //        sessionStorage.setItem("valid_balance", response.valid_balance); // 取现 参数valid_balance
        sessionStorage.setItem("mer_id", response.mer_id); // 商户号
        sessionStorage.setItem("corp_abbr", response.corp_abbr); // 商户简称
    }
}

// 点击商户二维码动作
function bindQrclick() {
    $("#merQrCodeBtn").on("touchstart", function() {
        var qrParameters = {
            "tl_mer_no": loginName
        }
        $.post(GetQrCodeServlet_URL, qrParameters, merQrCodeCallBack)
    });
}

// 分期消费点击事件
function bindSpendBtn() {
    $("#spendBtn").on("touchend", function() {
        setTimeout(function() {
            location.href = "./spending.html?lastPage=merchant";
        }, 200);
    });
}
// 取得商户二维ticket成功回调生成二维函数
function merQrCodeCallBack(msg) {
    console.dir(msg);
    if (msg.login_response) {
        haslogin(msg);
        return;
    }
    var tiket = msg.lfq_merinfo_response.mer_ticket;
    if (tiket) {
        $("#merQrCodeDialog img").attr("src", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + tiket);
        $("#qrcode-name").text(corpAbbr);
        showMerQrCode();
    } else {
        errorObject.showErroDialog("该商户未设置二维码");
    }

}

// 时间20161108160023 转为 2016-11-08 16:00:23
function changeDateTime(dateString, type) {
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录或已登出,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}

// 初始化变量
function initValue() {
    wc.QueryString.Initial();
    getSessionStorage();
    //    setSessionStorage();
    errorObject = new errorDialog();
    logoutObject = new Logout();
    sessionStorage.setItem("tradeDetail_page", 1);
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}

// 设置本地存储SessionStorage
function setSessionStorage() {
    sessionStorage.setItem("tltd_loginName", loginName);
    sessionStorage.setItem("tltd_token", token);
    sessionStorage.setItem("tltd_loginType", loginType);
    sessionStorage.setItem("tltd_openid", openid);
}